﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Data;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.Sellers;
namespace ZBJF.Warranty.BLL
{
    public partial class SellerEmployeeService
    {
        public SellerEmployee Login(string userAccount, string password, out bool flag)
        {
            var pass = CommonMethod.JiaMi(password);
            var list =
                LoadEntities(
                    c => c.UserAccount.Equals(userAccount, StringComparison.OrdinalIgnoreCase) &&
                         c.Password.Equals(pass, StringComparison.OrdinalIgnoreCase) && c.IsAvailable);
            var result = list.FirstOrDefault();
            flag = result != null;
            return result;
        }

        public bool IsExist(string userAccount, string userName)
        {
            var expressionWhere = PredicateExtensions.True<SellerEmployee>();
            expressionWhere = expressionWhere.And(o => o.UserAccount.Equals(userAccount)||o.UserName.Equals(userName));
            return LoadEntities(expressionWhere).Any();
        }

        public SellerEmployee LoadByPhone(string userPhone)
        {
            return LoadEntities(o => o.UserPhone.Equals(userPhone) || o.UserAccount.Equals(userPhone)).FirstOrDefault();
        }

        public IQueryable<SellerEmployee> GetPagingList(SellerEmployeeModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<SellerEmployee>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                string kw = model.Keyword.ToLower();
                expressionWhere = expressionWhere.And(o => o.UserName.ToLower().Contains(kw));
            }
            if (!string.IsNullOrEmpty(model.LikeName))
            {
                expressionWhere = expressionWhere.And(o => o.UserName.Contains(model.LikeName) || o.UserPhone.Contains(model.LikeName) || o.Email.Contains(model.LikeName) || o.UserAccount.Contains(model.LikeName) || o.CertificateNO.Contains(model.LikeName));
            }
            if (model.SellerId.HasValue)
            {
                expressionWhere = expressionWhere.And(o => o.SellerId==model.SellerId.Value);
            }
            if (string.IsNullOrEmpty(model.iSorts))
            {
                model.iSorts = "CreateTime desc";
            }
            return LoadPagerEntities<SellerEmployee>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }

        public SellerEmployee LoadByUserAccount(string userAccount)
        {
            return LoadEntities(o => o.UserAccount.Equals(userAccount)).FirstOrDefault();
        }

        public SellerEmployee LoadByID(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
        public List<SellerEmployee> LoadBySellerID(long sellerID)
        {
            return LoadEntities(o => o.SellerId == sellerID).ToList();
        }

        /// <summary>
        /// 验证密码是否正确
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ValidatePassword(long id, string password)
        {
            var employee = LoadByID(id);
            if (employee != null)
                return employee.Password.Equals(CommonMethod.JiaMi(password));
            return false;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newPassword"></param>
        /// <param name="modifyBy"></param>
        /// <returns></returns>
        public bool ModifyPassword(long id, string newPassword, string modifyBy)
        {
            var employee = LoadByID(id);
            if (employee != null)
            {
                employee.Password = CommonMethod.JiaMi(newPassword);
                employee.LastModifyBy = modifyBy;
                employee.LastModifyTime = DateTime.Now;
                return ServiceFactory.SellerEmployeeServiceInstance.UpdateEntities(employee);
            }
            return false;
        }

    }
}
